package main

import (
	"strconv"

	"gitee.com/konyshe/gogo"
	"github.com/google/gopacket"
	"github.com/google/gopacket/pcap"
)

func LoadFromPcap(filename string) {
	// Open file instead of device
	handle, err := pcap.OpenOffline(filename)
	gogo.CheckError(err)
	defer handle.Close()

	filter := "udp and dst port " + strconv.Itoa(M_RECV_PORT)
	gogo.CheckError(handle.SetBPFFilter(filter))
	gogo.LogInfo("Listen filter: ", filter)

	// Loop through packets in file
	packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
	for packet := range packetSource.Packets() {
		// When iterating through packet.Layers() above,
		// if it lists Payload layer then that is the same as
		// this applicationLayer. applicationLayer contains the payload
		applicationLayer := packet.ApplicationLayer()
		if applicationLayer != nil && pProPayloadTask != nil {
			pProPayloadTask.Push(&ProPayloadData{applicationLayer.Payload(), len(applicationLayer.Payload()), nil})
		}
	}
}
